from django.db import models

from apps.case.models import Case
from apps.users.models import UserProfile


# Create your models here.

class Steps(models.Model):
    """
    测试步骤模型
    """
    type = (
        ('interface', 'http接口'),
        ('app', '移动端'),
        ('web', 'Selenium'),
        ('sql', '数据库')
        )
    step_num = models.IntegerField(default=1, verbose_name=u'顺序号')
    name = models.CharField(max_length=18, verbose_name=u'名称')
    case = models.ForeignKey(Case, related_name='steps', on_delete=models.CASCADE, null=True, blank=True)
    step_info = models.CharField(max_length=200, null=True, blank=True, verbose_name='步骤信息')
    type = models.CharField(choices=type, default='interface', max_length=20, verbose_name=u'类型')
    params = models.CharField(max_length=500, null=True, blank=True, verbose_name=u'请求参数')
    expect = models.CharField(max_length=500, null=True, blank=True, verbose_name=u'结果校验')
    return_values = models.CharField(max_length=500, default=None, null=True, blank=True, verbose_name=u'返回值')
    add_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建时间')
    edit_time = models.DateTimeField(auto_now=True, verbose_name=u'修改时间')

    class Meta:
        db_table = 't_step'
        verbose_name = u'操作步骤库'
        verbose_name_plural = verbose_name
        ordering = ['-step_num']

    def __str__(self):
        return self.name

    def to_json(self):
        import json
        return json.dumps(dict([(attr, getattr(self, attr)) for attr in [f.name for f in self._meta.fields]]))
